﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using NHibernate;
using NHibernate.Linq;

using Common.Model;
using Common.Error;
using OpenAPI.Model.API;
using OpenAPI.IDAL.API;
using DBUtility;

namespace OpenAPI.NhiDAL.API
{
    public class DocumentDAL : IDocument
    {


        /// <summary>
        /// 获取文档
        /// </summary>
        /// <param name="doc_cd"></param>
        /// <returns></returns>
        public DocumentInfo Get(string doc_cd)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            DocumentInfo doc = session.Query<DocumentInfo>().SingleOrDefault(d => d.DOC_CD == doc_cd);
            NHibernateHelper.CloseSession();

            return doc;
        }

        /// <summary>
        /// 获取文档List
        /// </summary>
        /// <returns></returns>
        public IList<DocumentInfo> List()
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            IList<DocumentInfo> docs = (from d in session.Query<DocumentInfo>()
                                        select d).ToList();
            NHibernateHelper.CloseSession();

            return docs;
        }

        /// <summary>
        /// 新增文档
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool Add(DocumentInfo doc, out ErrorType error)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            bool result_stat = false;
            error = null;

            DocumentInfo exists_doc = session.Query<DocumentInfo>().SingleOrDefault(d => d.DOC_CD == doc.DOC_CD);

            if (exists_doc == null)
            {
                using (ITransaction trans = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(doc);
                        session.Flush();
                        trans.Commit();

                        result_stat = true;
                        error = null;
                    }
                    catch
                    {
                        trans.Rollback();

                        result_stat = false;
                        error = ErrorHelper.NewError("", "Database Error");
                    }
                }
            }
            else
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Exists Document[" + doc.DOC_CD + "]");
            }
            NHibernateHelper.CloseSession();

            return result_stat;
        }


        /// <summary>
        /// 修改文档
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool Modify(DocumentInfo doc, out ErrorType error)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            bool result_stat = false;
            error = null;

            DocumentInfo exists_doc = session.Query<DocumentInfo>().SingleOrDefault(d => d.DOC_CD == doc.DOC_CD);

            if (exists_doc != null)
            {
                using (ITransaction trans = session.BeginTransaction())
                {
                    try
                    {
                        exists_doc.DOC_SUBJECT = doc.DOC_SUBJECT;
                        exists_doc.DOC_DATETIME = doc.DOC_DATETIME;
                        exists_doc.DOC_AUTHOR = doc.DOC_AUTHOR;
                        exists_doc.DOC_TAGS = doc.DOC_TAGS;
                        exists_doc.DOC_BODY = doc.DOC_BODY;

                        session.Update(exists_doc);
                        session.Flush();
                        trans.Commit();

                        result_stat = true;
                        error = null;
                    }
                    catch
                    {
                        trans.Rollback();

                        result_stat = false;
                        error = ErrorHelper.NewError("", "Database Error");
                    }
                }
            }
            else
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Not Exists Document[" + doc.DOC_CD + "]");
            }
            NHibernateHelper.CloseSession();

            return result_stat;
        }

        /// <summary>
        /// 删除文档
        /// </summary>
        /// <param name="doc_cd"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool Delete(string doc_cd, out ErrorType error)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            bool result_stat = false;
            error = null;

            DocumentInfo exists_doc = session.Query<DocumentInfo>().SingleOrDefault(d => d.DOC_CD == doc_cd);

            if (exists_doc != null)
            {
                using (ITransaction trans = session.BeginTransaction())
                {
                    try
                    {
                        session.Delete(exists_doc);
                        session.Flush();
                        trans.Commit();

                        result_stat = true;
                        error = null;
                    }
                    catch
                    {
                        trans.Rollback();

                        result_stat = false;
                        error = ErrorHelper.NewError("", "Not exists document[" + doc_cd + "]");
                    }
                }
            }
            else
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Database Error");
            }
            NHibernateHelper.CloseSession();

            return result_stat;
        }

    }
}
